<?php

/*====================================================
 Author: RooTM
------------------------------------------------------
 Web-site: http://weboss.net/
=====================================================*/

@error_reporting ( E_ALL ^ E_WARNING ^ E_NOTICE );
@ini_set ( 'display_errors', true );
@ini_set ( 'html_errors', false );
@ini_set ( 'error_reporting', E_ALL ^ E_WARNING ^ E_NOTICE );

@session_start();

define( 'DATALIFEENGINE', true );
define( 'ROOT_DIR', '../../../..' );
define( 'ENGINE_DIR', '../../..' );

include ENGINE_DIR.'/data/config.php';

if ($config['http_home_url'] == "") {

	$config['http_home_url'] = explode("engine/modules/iChat/ajax/admin.php", $_SERVER['PHP_SELF']);
	$config['http_home_url'] = reset($config['http_home_url']);
	$config['http_home_url'] = "http://".$_SERVER['HTTP_HOST'].$config['http_home_url'];

}

require_once ENGINE_DIR.'/classes/mysql.php';
require_once ENGINE_DIR.'/data/dbconfig.php';
require_once ENGINE_DIR . '/modules/functions.php';

require_once ENGINE_DIR.'/modules/sitelogin.php';

if(($member_id['user_group'] != 1)) {die ("error");}

@header( "Content-type: text/css; charset=" . $config['charset'] );

include ENGINE_DIR . '/modules/iChat/data/config.php';

if($_POST['action'] == "save"){

$save_cfg = $_POST['save_cfg'];

$save_cfg['skin_tpl'] = convert_unicode( $save_cfg['skin_tpl'] , $config['charset']  );
$save_cfg['rules_tpl'] = convert_unicode( $save_cfg['rules_tpl'] , $config['charset']  );
$save_cfg['message_tpl'] = convert_unicode( $save_cfg['message_tpl'] , $config['charset']  );

if( function_exists( "get_magic_quotes_gpc" ) && get_magic_quotes_gpc() ){
 $save_cfg['skin_tpl'] = stripslashes( $save_cfg['skin_tpl'] );
 $save_cfg['rules_tpl'] = stripslashes( $save_cfg['rules_tpl'] );
 $save_cfg['message_tpl'] = stripslashes( $save_cfg['message_tpl'] );
}

	$handle = fopen( ENGINE_DIR . '/modules/iChat/template/skin.tpl', "w" );
	fwrite( $handle, $save_cfg['skin_tpl'] );
	fclose( $handle );

	$handle = fopen( ENGINE_DIR . '/modules/iChat/template/rules.tpl', "w" );
	fwrite( $handle, $save_cfg['rules_tpl'] );
	fclose( $handle );

	$handle = fopen( ENGINE_DIR . '/modules/iChat/template/message.tpl', "w" );
	fwrite( $handle, $save_cfg['message_tpl'] );
	fclose( $handle );

unset( $save_cfg['skin_tpl'] );
unset( $save_cfg['rules_tpl'] );
unset( $save_cfg['message_tpl'] );

$save_cfg['version'] = "5.0";
	
if(!is_numeric(trim($save_cfg['sum_msg'])) OR trim($save_cfg['sum_msg']) <= 0) { $save_cfg['sum_msg'] = 10;}
if(!is_numeric(trim($save_cfg['max_text'])) OR trim($save_cfg['max_text']) <= 0) { $save_cfg['max_text'] = 300;}
if(!is_numeric(trim($save_cfg['refresh'])) OR trim($save_cfg['refresh']) <= 0) { $save_cfg['refresh'] = 15;}
if(!is_numeric(trim($save_cfg['max_word'])) OR trim($save_cfg['max_word']) <= 0) { $save_cfg['max_word'] = 33;}
if(!is_numeric(trim($save_cfg['cron_clean'])) OR trim($save_cfg['cron_clean']) <= 0) { $save_cfg['cron_clean'] = 15;}
if(!is_numeric(trim($save_cfg['stop_flood'])) OR trim($save_cfg['stop_flood']) < 0) { $save_cfg['stop_flood'] = 30;}

$save_cfg = $save_cfg + $chat_cfg;

	$handler = fopen( ENGINE_DIR . '/modules/iChat/data/config.php', "w" );
	
	fwrite( $handler, "<?PHP \n\n//iChat Configurations\n\n\$chat_cfg = array (\n\n" );
	foreach ( $save_cfg as $name => $value ) {
		
          $value = str_replace( "%3D", "=", $value );

	     $value = str_replace( "$", "&#036;", $value );
		$value = str_replace( "{", "&#123;", $value );
		$value = str_replace( "}", "&#125;", $value );
		
		$name = str_replace( "$", "&#036;", $name );
		$name = str_replace( "{", "&#123;", $name );
		$name = str_replace( "}", "&#125;", $name );

		fwrite( $handler, "'{$name}' => \"{$value}\",\n\n" );
	
	}
	fwrite( $handler, ");\n\n?>" );
	fclose( $handler );

include ENGINE_DIR . '/modules/iChat/data/config.php';

}

$js_data = @file_get_contents(ENGINE_DIR . '/modules/iChat/template/js/admin.js');

$skin_tpl = htmlspecialchars( stripslashes(@file_get_contents(ENGINE_DIR . '/modules/iChat/template/skin.tpl' ) ) );
$rules_tpl = htmlspecialchars( stripslashes( @file_get_contents(ENGINE_DIR . '/modules/iChat/template/rules.tpl' ) ) );
$message_tpl = htmlspecialchars( stripslashes(@file_get_contents(ENGINE_DIR . '/modules/iChat/template/message.tpl' ) ) );

$content = <<<HTML
<script language="javascript" type="text/javascript">
$js_data
</script>

<div id="content">

<div id="iChat_cfg">

<input class="bbcodes" style="font-size: 11px; float: left;" title="Редактировать шаблоны" onclick="OpenTab(); return false;" type="button" value="Редактировать шаблоны" />

<br><br>

<b>Количество собщений в блоке:</b>
<input id="cfg01" type=text style="text-align: center;"  value="{$chat_cfg['sum_msg']}" size=10><br />
     <hr />
<b>Максимальное количество символов в собщении:</b>
<input id="cfg12" type=text style="text-align: center;"  value="{$chat_cfg['max_text']}" size=10><br />
     <hr />
<b>Формат времени для сообщений:</b>
<input id="cfg13" type=text style="text-align: center;"  value="{$chat_cfg['format_date']}" size=10><br />
     <hr />
<b>Автообновления блока в секундах:</b>
<input id="cfg02" type=text style="text-align: center;" value="{$chat_cfg['refresh']}" size=10><br />
     <hr />
<b>Защита от флуда в секундах:</b>
<input id="cfg03" type=text style="text-align: center;"  value="{$chat_cfg['stop_flood']}" size=10><br />
     <hr />
<b>Автоматическая разбивка длинных слов:</b>
<input id="cfg04" type=text style="text-align: center;"  value="{$chat_cfg['max_word']}" size=10><br />
     <hr />
<b>Количество собщений удалять(CRON):</b>
<input id="cfg05" type=text style="text-align: center;"  value="{$chat_cfg['cron_clean']}" size=10><br />
     <hr />
<b>Заблокировать определенные BBCODE:</b>
<input id="cfg07" type=text style="text-align: center;"  value="{$chat_cfg['stop_bbcode']}" size=28><br />
     <hr />
<b>Смайлики:</b>
<input id="cfg06" type=text style="text-align: center;"  value="{$chat_cfg['smiles']}" size=55><br />
     <hr />
<b>Цвета раскраски имен пользователей:</b>
<input id="cfg08" type=text style="text-align: center;"  value="{$chat_cfg['groups_color']}" size=30><br />
 
</div>

		
<div id="iChat_tpl" style="display:none">
   
<input class="bbcodes" style="font-size: 11px; float: left;" title="Редактировать настройки" onclick="OpenTab(); return false;" type="button" value="Редактировать настройки" />

<br><br>

<b>skin.tpl:</b>
<textarea id="cfg14" style="width:100%;height:90px;background-color:lightyellow;">$skin_tpl</textarea>
    <hr />
<b>rules.tpl:</b>
<textarea id="cfg15" style="width:100%;height:90px;background-color:lightyellow;">$rules_tpl</textarea>
    <hr />
<b>message.tpl:</b>
<textarea id="cfg16" style="width:100%;height:90px;background-color:lightyellow;">$message_tpl</textarea>

</div>

<div id="progres"></div>

</div>
HTML;

if($_POST['check'] == "updates") {
$data = @file_get_contents("http://weboss.net/iChat/updates.php?version=".$chat_cfg['version']);

if ( !$data ) $data = "Не удалось подключится к удаленному серверу, возможно ваш сервер не поддерживает удаленные соединения, либо произошел сбой в работе наших серверов. Повторите попытку немного позднее ..."; else {

	if ($config['charset'] == "utf-8") $data = iconv("windows-1251", "utf-8", $data);

}
echo <<<HTML
<script language="JavaScript" type="text/javascript">
DLEalert('$data', 'iChat updates')
</script>
HTML;
echo $content;
die();
}

if($_POST['action'] == "save" OR $_POST['action'] == "clear"){

if($_POST['action'] == "clear"){$db->query("TRUNCATE TABLE " . PREFIX . "_iChat");}

echo $content;
clear_cache( 'iChat_' );
}

echo "<div id='ECPU' title='Настройка чата ( Версия: {$chat_cfg['version']} ) <a href=\"http://weboss.net/\" target=\"_blank\" style=\"text-decoration: none; font-size: 9px;\"></a>' style='display:none'>{$content}</div>";

?>